#include "demo_runner.h"
#include "utils_lib.h"
#include <iostream>
#include <vector>

const char* DemoRunner::TEST_FILE_PATH = "test_file.txt";

DemoRunner::DemoRunner() {
    // 构造函数
}

DemoRunner::~DemoRunner() {
    // 清理测试文件
    cleanupTestFile();
}

void DemoRunner::demonstrateStringUtils() {
    using namespace Utils;
    
    std::string testString = "  Hello, SCons Builder System!  ";
    
    std::cout << "原始字符串: '" << testString << "'" << std::endl;
    std::cout << "去除空白: '" << StringUtils::trim(testString) << "'" << std::endl;
    std::cout << "转大写: '" << StringUtils::toUpper(testString) << "'" << std::endl;
    std::cout << "转小写: '" << StringUtils::toLower(testString) << "'" << std::endl;
    
    std::string csvString = "apple,banana,cherry,date";
    auto parts = StringUtils::split(csvString, ',');
    std::cout << "\n分割字符串 '" << csvString << "':" << std::endl;
    for (const auto& part : parts) {
        std::cout << "  - " << part << std::endl;
    }
    
    std::string joined = StringUtils::join(parts, " | ");
    std::cout << "重新连接: " << joined << std::endl;
    
    std::string filename = "document.pdf";
    std::cout << "\n文件名检查:" << std::endl;
    std::cout << "  '" << filename << "' 以 'doc' 开头: " << (StringUtils::startsWith(filename, "doc") ? "是" : "否") << std::endl;
    std::cout << "  '" << filename << "' 以 '.pdf' 结尾: " << (StringUtils::endsWith(filename, ".pdf") ? "是" : "否") << std::endl;
}

void DemoRunner::demonstrateFileUtils() {
    using namespace Utils;
    
    // 创建测试文件
    createTestFile();
    
    std::cout << "文件操作演示:" << std::endl;
    std::cout << "  测试文件存在: " << (FileUtils::fileExists(TEST_FILE_PATH) ? "是" : "否") << std::endl;
    
    // 读取文件内容
    std::string content = FileUtils::readFile(TEST_FILE_PATH);
    std::cout << "  文件内容: " << content << std::endl;
    
    // 获取文件信息
    std::cout << "  文件扩展名: " << FileUtils::getFileExtension(TEST_FILE_PATH) << std::endl;
    std::cout << "  文件名: " << FileUtils::getFileName(TEST_FILE_PATH) << std::endl;
    
    // 列出当前目录文件
    auto files = FileUtils::listFiles(".");
    std::cout << "  当前目录文件数量: " << files.size() << std::endl;
    if (!files.empty()) {
        std::cout << "  前几个文件:";
        for (size_t i = 0; i < std::min(size_t(3), files.size()); ++i) {
            std::cout << " " << files[i];
        }
        std::cout << std::endl;
    }
}

void DemoRunner::demonstrateTimeUtils() {
    using namespace Utils;
    
    std::cout << "时间工具演示:" << std::endl;
    std::cout << "  当前时间: " << TimeUtils::getCurrentTime() << std::endl;
    std::cout << "  自定义格式: " << TimeUtils::formatTime("%Y年%m月%d日 %H:%M:%S") << std::endl;
    
    long long timestamp = TimeUtils::getCurrentTimestamp();
    std::cout << "  当前时间戳: " << timestamp << std::endl;
    std::cout << "  时间戳转换: " << TimeUtils::timestampToString(timestamp) << std::endl;
}

void DemoRunner::printSeparator() {
    std::cout << "----------------------------------------" << std::endl;
}

void DemoRunner::createTestFile() {
    using namespace Utils;
    
    std::string content = "这是一个测试文件\n用于演示文件工具功能\n";
    FileUtils::writeFile(TEST_FILE_PATH, content);
}

void DemoRunner::cleanupTestFile() {
    using namespace Utils;
    
    if (FileUtils::fileExists(TEST_FILE_PATH)) {
        // 注意: 这里只是演示，实际删除文件需要使用系统调用
        // 为了简单起见，我们不在这里实现文件删除
    }
}